#! /usr/bin/python3
from requests import Request, Session
from eggs.sanbayaccount import SB_USER, SB_PWD
import csv
import sys

login_url = 'https://www.shanbay.com/api/v1/account/login/web/'


get_familiar_words = 'https://www.shanbay.com/api/v1/bdc/library/familiar/?page=%s&_=1493170715107'
get_master_words = 'https://www.shanbay.com/api/v1/bdc/library/master/?page=%s&_=1493170715107'
get_fresh_words = 'https://www.shanbay.com/api/v1/bdc/library/fresh/?page=%s&_=1493170715107'
get_today_words = 'https://www.shanbay.com/api/v1/bdc/library/today/?page=%s&_=1493170715107'
get_resolved_words = 'https://www.shanbay.com/api/v1/bdc/library/resolved/?page=%s&_=1558105833641'

words_urls = ['https://www.shanbay.com/api/v1/bdc/library/familiar/?page=%s',
              'https://www.shanbay.com/api/v1/bdc/library/master/?page=%s',
              'https://www.shanbay.com/api/v1/bdc/library/fresh/?page=%s',
              'https://www.shanbay.com/api/v1/bdc/library/today/?page=%s',
              'https://www.shanbay.com/api/v1/bdc/library/resolved/?page=%s'
             ]

s = Session()
req = Request('PUT',  login_url, data= {"username":SB_USER,"password":SB_PWD})

prepped = s.prepare_request(req)

resp = s.send(prepped)

#print(resp.text)

#if not login():
#    sys.exit(0)


def login():
    req = Request('PUT',  login_url, data= {"username":SB_USER,"password":SB_PWD})
    prepped = sanbay.prepare_request(req)
    resp = sanbay.send(prepped)
    if resp.msg == "SUCCESS":
        return True
    else:
        print('未能登录扇贝网。')
        print(resp.msg)
        return False


def get_wordlist(url):
    pass

def get_wordpages(url, ):
    pass


def parse_wordpage(url):
    pass

def save_word(file, words):
    pass


def export_my_word_list(filename):

    word_set = set([])    
        
    for word_list_url in words_urls:
        print('get url: %s' % word_list_url)
        first = s.send(s.prepare_request(Request('GET', word_list_url % 1))).json()
        page_count = (first["data"]["total"] // 10) + 1
        for i in range(1, page_count + 1):
            print('get page : %s/%s' % (i,page_count))
            learn_resp = s.send(s.prepare_request(Request('GET', word_list_url % i)))
            resp_words = learn_resp.json()

            for w in resp_words["data"]["objects"]:
                word_set.add(w["content"].lower())
    
    with open(filename, 'w') as listfile:
        listfile.writelines([w + "\n" for w in word_set])

        
def export_my_words(filename, wurl):
    
    with open(filename, 'w') as csvfile:
        fieldnames = ['content', 'definition']
        writer = csv.DictWriter(csvfile, fieldnames=fieldnames, extrasaction='ignore')
        writer.writeheader()

        first = s.send(s.prepare_request(Request('GET', wurl % 1))).json()

        page_total = (first["data"]["total"] // 10) + 1

        for i in range(1, page_total + 1):
            print('get page : %s' % i)
            learn_resp = s.send(s.prepare_request(Request('GET', wurl % i)))
            resp_words = learn_resp.json()

            for w in resp_words["data"]["objects"]:
                w["content"] = '"' + w["content"] + '"'
                w['definition'] = '"' + w['definition'].replace('\n','').replace('\r','') + '"'
                w['definition'] = w['definition'].replace('""','"')
                writer.writerow(w)
                print('write row : %s,  %s' % (w['content'],w['definition']))
                #print(w)

# export_my_words("fw.csv", get_familiar_words)
# export_my_words("mw.csv", get_master_words)
export_my_word_list('words_list')
